﻿using System;
using System.Collections.Generic;
using Common;
using Common.CustomException;

namespace SpecFlowTfsLinker.Entities
{
    public partial class UserStory
    {
        public class UsBuilder
        {
            private readonly UserStory _us;

            internal UsBuilder()
            {
                _us = new UserStory();
            }

            public UsBuilder Title(string title)
            {
                _us.Title = title;
                return this;
            }

            public UsBuilder Id(int id)
            {
                _us.Id = id;
                return this;
            }

            public UsBuilder Description(string description)
            {
                _us.Description = description;
                return this;
            }

            public UsBuilder AddTestCase(TestCase testCase)
            {
                if (_us.TestCases == null)
                    _us.TestCases = new List<TestCase>();

                _us.TestCases.Add(testCase);
                return this;
            }

            public UserStory Build()
            {
                if (UsHasError())
                    throw new UserStoryException(Resource.US_BUILD_FAILED);

                if (UsHasNoTestCase())
                    throw new UserStoryException(Resource.US_BUILD_FAIL_ONE_TEST_CASE);

                return _us;
            }

            private bool UsHasNoTestCase()
            {
                return _us.TestCases == null || _us.TestCases.Count == 0;
            }

            private bool UsHasError()
            {
                return _us.Id <= 0 || String.IsNullOrEmpty(_us.Description) || String.IsNullOrEmpty(_us.Title);
            }
        }
    }
}